package com.yunbiao.ybbusstopplate.base;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.example.library_hy_advert.system.Path;
import com.yunbiao.ybbusstopplate.refactoring.utils.L;
import com.yunbiao.ybbusstopplate.utils.ActivityUtils;

import java.io.*;
import java.lang.Thread.UncaughtExceptionHandler;

// 创建服务用于捕获崩溃异常  
public class MyUncaughtExceptionHandler implements UncaughtExceptionHandler {
    private static final String TAG = "MyUncaughtExceptionHand";
    private final Application application;

    public MyUncaughtExceptionHandler(Application application) {
        this.application = application;
    }

    @Override
    public void uncaughtException(@NonNull Thread thread, @NonNull Throwable ex) {
        // 保存错误日志
        L.e(TAG, ex);
        //5秒钟后重启应用
        startRestart();
        // 关闭当前应用
        ActivityUtils.getInstance().finishAll();
        finishProgram();
    }

    private void startRestart(){
        stopRestart();

        Intent intent = new Intent();
        String packgeName = application.getPackageName();
        // 参数1：包名，参数2：程序入口的activity
        intent.setClassName(packgeName, packgeName + ".ui.activity.SplashActivity");
        PendingIntent restartIntent = PendingIntent.getActivity(application, 888888, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        // 5秒钟后重启应用
        AlarmManager mgr = (AlarmManager) application.getSystemService(Context.ALARM_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mgr.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, restartIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mgr.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, restartIntent);
        } else {
            mgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, restartIntent);
        }
    }

    private void stopRestart(){
        Intent intent = new Intent();
        String packgeName = application.getPackageName();
        // 参数1：包名，参数2：程序入口的activity
        intent.setClassName(packgeName, packgeName + ".ui.activity.SplashActivity");
        PendingIntent restartIntent = PendingIntent.getActivity(application, 888888, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        if (restartIntent!=null){
            AlarmManager mgr = (AlarmManager) application.getSystemService(Context.ALARM_SERVICE);
            if (mgr!=null){
                mgr.cancel(restartIntent);
            }
        }
    }

    // 结束线程,一般与finishAllActivity()一起使用
    // 例如: finishAllActivity;finishProgram();
    private void finishProgram() {
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
